<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\BaseController;
use app\common\lib\Show;
use think\exception\ValidateException;
use think\facade\Request;
use app\common\business\Sms as BusiSms;

class Sms extends BaseController
{
    public function code(): Object
    {
        if (!Request::isPost()) {
            return Show::error('非法请求', config('status.request_action_error'));
        }
        // 获取提交数据
        $data = [
            'phone' => Request::param('phone_number', '', 'trim')
        ];

        // 验证提交数据
        try{
            validate(\app\api\validate\User::class)->scene('send_code')->check($data);
        } catch (ValidateException $e) {
            return Show::error($e->getError());
        }

        // 发送验证码
        $sendCode = BusiSms::sendCode($data['phone'], 6, 'ali');
        if ($sendCode) {
            return Show::success(['code' => $sendCode], '验证码发送成功');
        }
        return Show::error('验证码发送失败');
    }
}
